#include "StdAfx.h"
#include "ImageRecord.h"
#include "ImageMan.h"

#pragma comment (lib,"vfw32.lib")
#pragma comment (lib,"winmm.lib")

ImageRecord::ImageRecord(void)
{
	m_pfile      = NULL;
	m_ps         = NULL; 
	m_pComStream = NULL;

	m_nBufSize = 0; 
	m_pBuffer  = NULL;
}

ImageRecord::~ImageRecord(void)
{
	StopRecord();
}

int ImageRecord::ImgRecord(zkImgBitInfo *pImgBitInfo)
{
	switch( pImgBitInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{
			
		}
		break;
	case eImage_Define_Img16:
		{
			
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	return 0;
}

int ImageRecord::StartRecord(char *pFileName)
{
	StopRecord();

	do 
	{
		AVIFileInit(); 

		ImageMan *pMan = (ImageMan*)this;

		int nWidth  = pMan->m_xDisp.m_nDispW;
		int nHeight = pMan->m_xDisp.m_nDispH;
		

		BYTE sBuffer[ sizeof(BITMAPINFOHEADER) + 2000 ] = {0};
// 		BITMAPINFOHEADER *pBInfo = (BITMAPINFOHEADER *)sBuffer;
// 		pBInfo->biBitCount       = 8;
// 		pBInfo->biClrImportant   = 0;
// 		pBInfo->biCompression    = 0;
// 		pBInfo->nHeight         = nHeight;
// 		pBInfo->biPlanes         = 1;
// 		pBInfo->biSize           = sizeof(BITMAPINFOHEADER);
// 		pBInfo->biSizeImage      = nWidth*nHeight;
// 		pBInfo->nWidth          = nWidth;
// 		pBInfo->biXPelsPerMeter  = 0;
// 		pBInfo->biYPelsPerMeter  = 0;
// 		pBInfo->biClrUsed        = 256;
		LPBITMAPINFO   pbmi8 = (LPBITMAPINFO)sBuffer;
		//bitmap   infoheader 
		pbmi8-> bmiHeader.biSize           =   sizeof(   BITMAPINFOHEADER   ); 
		pbmi8-> bmiHeader.biWidth          =   nWidth; 
		pbmi8-> bmiHeader.biHeight         =   nHeight; 
		pbmi8-> bmiHeader.biPlanes         =   1; 
		pbmi8-> bmiHeader.biBitCount       =   8;   //   bits   per   pixel 
		pbmi8-> bmiHeader.biCompression    =   BI_RGB;   //uncompressed   format 
		pbmi8-> bmiHeader.biSizeImage      =   0;   //   can   be   0   for   BI_RGB   bitmaps 
		pbmi8-> bmiHeader.biXPelsPerMeter  =   0; 
		pbmi8-> bmiHeader.biYPelsPerMeter  =   0; 
		pbmi8-> bmiHeader.biClrUsed        =   256; 
		pbmi8-> bmiHeader.biClrImportant   =   0; 
		//color   table 
		for(int i=0;i<256;++i)   
		{ 
			//8-bits   grayscale 
			pbmi8->bmiColors[i].rgbBlue      = pbmi8->bmiColors[i].rgbGreen  =  pbmi8->bmiColors[i].rgbRed = i; 
			pbmi8->bmiColors[i].rgbReserved  = 0; 
		} 


		AVIFileOpen(&m_pfile,pFileName,OF_WRITE|OF_CREATE,NULL);
		if( NULL == m_pfile )
		{
			break;
		}

		memset(&m_strhdr, 0, sizeof(m_strhdr));
		m_strhdr.fccType    = streamtypeVIDEO;
		m_strhdr.fccHandler = 0;
		m_strhdr.dwScale    = 1;
		m_strhdr.dwRate     = 5;
		m_strhdr.dwSuggestedBufferSize = nWidth*nHeight;
		SetRect(&m_strhdr.rcFrame,0,0,nWidth,nHeight);
		AVIFileCreateStream(m_pfile,&m_ps,&m_strhdr); 
		if( NULL == m_ps )
		{
			break;
		}

		AVICOMPRESSOPTIONS FAR * opts[1] = {&m_CompressOption}; 

		opts[0]->fccType    =  streamtypeVIDEO;
		//opts[0]->fccHandler =  mmioStringToFOURCC("MSVC", 0);

		if( mmioFOURCC('D','I','B',' ') == m_xCOMPVARS.fccHandler )
		{
			opts[0]->fccHandler =  m_xCOMPVARS.fccHandler;
		}
		else
		{
			opts[0]->fccHandler =  mmioStringToFOURCC("MSVC", 0);
		}
		
		opts[0]->dwQuality  = 7500;
		opts[0]->dwBytesPerSecond = 0;
		opts[0]->dwFlags  = AVICOMPRESSF_VALID || AVICOMPRESSF_KEYFRAMES;
		opts[0]->lpFormat = 0;
		opts[0]->cbFormat = 0;
		opts[0]->dwInterleaveEvery = 50;
		opts[0]->dwKeyFrameEvery = 50;

		AVIMakeCompressedStream(&m_pComStream,m_ps,&m_CompressOption,NULL); 
		if( NULL == m_pComStream )
		{
            break;
		}
		//AVIStreamSetFormat(m_pComStream,0,pBInfo,sizeof(BITMAPINFOHEADER));
		AVIStreamSetFormat( m_pComStream,0,(LPVOID)pbmi8,sizeof(BITMAPINFOHEADER)+(256*sizeof(RGBQUAD)));


		m_nFrames = 0;

		m_nBufSize = nWidth * nHeight + 1024;
		m_pBuffer = new BYTE[m_nBufSize];
		if( NULL == m_pBuffer )
		{
			break;
		}

		if( !DDThread::DDThdInit() )
		{
			break;
		}

		return 0;

	} while (false);

	StopRecord();

	return -1;
}

int ImageRecord::StopRecord()
{
	DDThread::DDThdUnInit();

	if( NULL != m_pComStream )
	{
		AVIStreamClose(m_pComStream);
		m_pComStream = NULL;
	}
	if( NULL != m_ps )
	{
		AVIStreamClose(m_ps);
		m_ps = NULL;
	}
	if(m_pfile != NULL)
	{
		AVIFileRelease(m_pfile);
	    m_pfile = NULL;
	}
	AVIFileExit();

	m_nBufSize = 0; 
	if( NULL != m_pBuffer )
	{
		delete[]m_pBuffer;
		m_pBuffer = NULL;
	}

	return 0;
}


int ImageRecord::DDThdDispatch(HANDLE hExitEvent)
{
	HRESULT    hr = 0; 

	int   nBufferSize = 0;
	BYTE *pBuffer     = m_pBuffer;

	while( DDThread::m_bThreadRun )
	{
		( (ImageMan*)this )->GetGrayBuffer(pBuffer,nBufferSize);

		hr = AVIStreamWrite(m_pComStream,m_nFrames ,1,pBuffer,nBufferSize,AVIIF_KEYFRAME,NULL,NULL);
		m_nFrames++;

		Sleep(50);
	}

	return 0;
}